using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
using EcsSiteDSTableAdapters;

namespace SiteECSWeb
{
    [DataObject]
    public class AuthoritiesBLL
    {
        private AuthoritiesTableAdapter _AuthoritiesAdapter = null;

        protected AuthoritiesTableAdapter AuthoritiesAdapter
        {
            get
            {
                if (_AuthoritiesAdapter == null)
                {
                    _AuthoritiesAdapter = new AuthoritiesTableAdapter();
                }
                return _AuthoritiesAdapter;
            }
        }

        #region SelectAuthorities
        [DataObjectMethodAttribute(DataObjectMethodType.Select, true)]
        public EcsSiteDS.AuthoritiesDataTable GetAuthoritiesRowRank()
        {
            return AuthoritiesAdapter.GetAuthoritiesRowRankData();
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.AuthoritiesDataTable GetAuthoritiesByAuthID(int Auth_ID)
        {
            return AuthoritiesAdapter.GetAuthoritiesDataByAuthID(Auth_ID);
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.AuthoritiesDataTable GetAuthoritiesByAuthTypeID(int AuthType_ID)
        {
            return AuthoritiesAdapter.GetAuthoritiesDataByAuthTypeID(AuthType_ID);
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public PagedDataSource GetAuthoritiesAsPagedDataSource
            (int pageIndex, int pageSize)
        {
            return GetAuthoritiesAsPagedDataSource(string.Empty, 0, pageIndex, pageSize);
        }
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public PagedDataSource GetAuthoritiesAsPagedDataSource
            (string AuthorName, int AuthorType,
            int pageIndex, int pageSize)
        {
            PagedDataSource pagedData = new PagedDataSource();

            if (string.IsNullOrEmpty(AuthorName) && AuthorType <=0 )
            {
                EcsSiteDS.AuthoritiesDataTable AuthorTable = AuthoritiesAdapter.GetAuthoritiesRowRankData();
                
                pagedData.DataSource = AuthorTable.Rows;
                pagedData.AllowPaging = true;
                pagedData.CurrentPageIndex = pageIndex;
                pagedData.PageSize = pageSize;
                return pagedData;
            }
            else
            {
                if (AuthorType > 0)
                {
                    EcsSiteDS.AuthoritiesDataTable AuthorTable =
                        AuthoritiesAdapter.GetAuthoritiesDataByAuthTypeID(AuthorType);            
                    pagedData.DataSource = AuthorTable.Rows;
                    pagedData.AllowPaging = true;
                    pagedData.CurrentPageIndex = pageIndex;
                    pagedData.PageSize = pageSize;
                    return pagedData;
                }
                return pagedData;
            }
        }

        #endregion

        #region InsertAuthorities
        [DataObjectMethodAttribute(DataObjectMethodType.Insert, true)]
        public int AddAuthorty(string Auth_Namear, string Auth_Addressar,
            string Auth_Nameen, string Auth_Addressen, string Auth_Phone,
            string Auth_Phone2, string Auth_Fax, string Auth_Fax2, string Auth_Email,
            string Auth_Website, int AuthType_ID,int? City_id)
        {
            
            EcsSiteDS.AuthoritiesDataTable AuthortyTable = new EcsSiteDS.AuthoritiesDataTable();
            EcsSiteDS.AuthoritiesRow AddAuthortyRow = AuthortyTable.NewAuthoritiesRow();

            if (string.IsNullOrEmpty(Auth_Addressar)) AddAuthortyRow.SetAuth_AddressarNull(); else AddAuthortyRow.Auth_Addressar = Auth_Addressar;
            if (string.IsNullOrEmpty(Auth_Nameen)) AddAuthortyRow.SetAuth_NameenNull(); else AddAuthortyRow.Auth_Nameen = Auth_Nameen;
            if (string.IsNullOrEmpty(Auth_Addressen )) AddAuthortyRow.SetAuth_AddressenNull(); else AddAuthortyRow.Auth_Addressen = Auth_Addressen;
            if (string.IsNullOrEmpty(Auth_Phone2 )) AddAuthortyRow.SetAuth_Phone2Null(); else AddAuthortyRow.Auth_Phone2 = Auth_Phone2;
            if (string.IsNullOrEmpty(Auth_Fax2) ) AddAuthortyRow.SetAuth_Fax2Null(); else AddAuthortyRow.Auth_Fax2 = Auth_Fax2;
            if (string.IsNullOrEmpty(Auth_Website )) AddAuthortyRow.SetAuth_WebsiteNull(); else AddAuthortyRow.Auth_Website = Auth_Website;
            if (string.IsNullOrEmpty(Auth_Website)) AddAuthortyRow.SetAuth_WebsiteNull(); else AddAuthortyRow.Auth_Website = Auth_Website;
            if (City_id == null) AddAuthortyRow.SetCity_idNull(); else AddAuthortyRow.City_id = City_id.Value;
            
            AddAuthortyRow.Auth_Namear = Auth_Namear;
            AddAuthortyRow.Auth_Phone = Auth_Phone;
            AddAuthortyRow.Auth_Fax = Auth_Fax;
            AddAuthortyRow.Auth_Email = Auth_Email;
            AddAuthortyRow.AuthType_ID = AuthType_ID;
            
            AuthortyTable.AddAuthoritiesRow(AddAuthortyRow);
           
            int rowsAffected = AuthoritiesAdapter.Update(AuthortyTable);
            
            return (rowsAffected == 1) ? AddAuthortyRow.Auth_ID : -1;
        }


        #endregion

        #region UpdateAuthorities
        [DataObjectMethodAttribute(DataObjectMethodType.Update, true)]
        public bool UpdateAuthorty(string Auth_Namear, string Auth_Addressar,
            string Auth_Nameen, string Auth_Addressen, string Auth_Phone,
            string Auth_Phone2, string Auth_Fax, string Auth_Fax2, string Auth_Email,
            string Auth_Website, int AuthType_ID,int? City_id, int Auth_ID)
        {
            EcsSiteDS.AuthoritiesDataTable AuthortyTable = AuthoritiesAdapter.GetAuthoritiesDataByAuthID(Auth_ID);
            if (AuthortyTable.Count == 0)
            {
                return false;
            }

            EcsSiteDS.AuthoritiesRow AuthortyRow = AuthortyTable[0];

            if (Auth_Addressar == null) AuthortyRow.SetAuth_AddressarNull(); else AuthortyRow.Auth_Addressar = Auth_Addressar;
            if (Auth_Nameen == null) AuthortyRow.SetAuth_NameenNull(); else AuthortyRow.Auth_Nameen = Auth_Nameen;
            if (Auth_Addressen == null) AuthortyRow.SetAuth_AddressenNull(); else AuthortyRow.Auth_Addressen = Auth_Addressen;
            if (Auth_Phone2 == null) AuthortyRow.SetAuth_Phone2Null(); else AuthortyRow.Auth_Phone2 = Auth_Phone2;
            if (Auth_Fax2 == null) AuthortyRow.SetAuth_Fax2Null(); else AuthortyRow.Auth_Fax2 = Auth_Fax2;
            if (Auth_Website == null) AuthortyRow.SetAuth_WebsiteNull(); else AuthortyRow.Auth_Website = Auth_Website;
            if (City_id == null) AuthortyRow.SetCity_idNull(); else AuthortyRow.City_id = City_id.Value;

            AuthortyRow.Auth_Namear = Auth_Namear;
            AuthortyRow.Auth_Phone = Auth_Phone;
            AuthortyRow.Auth_Fax = Auth_Fax;
            AuthortyRow.Auth_Email = Auth_Email;
            AuthortyRow.AuthType_ID = AuthType_ID;            

            
            int rowsAffected = AuthoritiesAdapter.Update(AuthortyRow);
            return rowsAffected == 1;
        }

        #endregion

        #region DeleteAuthorities
        [DataObjectMethodAttribute(DataObjectMethodType.Delete, true)]
        public bool DeleteAuthorty(int Auth_ID)
        {
            int rowsAffected = AuthoritiesAdapter.Delete(Auth_ID);
            return rowsAffected == 1;
        }
        #endregion


    }

}
